package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> add(@RequestBody DishDTO dishDto) {
        dishService.addWithFlavor(dishDto);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO, HttpServletRequest request) {
        /*String requestURI = request.getRequestURI();
        log.info("requestURI是{}",requestURI);
        int i = requestURI.lastIndexOf("&");
        log.info("i是{}",i);
        String[] split = requestURI.substring(i).split("=");
        try {
            String s = split[1];
        } catch (Exception e) {
            log.info("前端的url错误");
            dishPageQueryDTO.setStatus(null);
        }*/
        return Result.success(dishService.page(dishPageQueryDTO));
    }

    /**
     * 根据id批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteByids(Long[] ids) {
        dishService.deleteByids(ids);
        //将dish_开头的key全部删除
        clearCache("dish_*");
        return Result.success();
    }


    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        return Result.success(dishService.getByIdWithFlavor(id));
    }

    /**
     * 更新菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> update(@RequestBody DishDTO dishDto){
        dishService.update(dishDto);
        //涉及到一个分类下菜品变多了，另一个分类下菜品变少了，简单处理，直接删除以dish_开头的所有缓存
        clearCache("dish_*");
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        return Result.success(dishService.list(categoryId));
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用菜品")
    public Result startOrStop(@PathVariable Integer status,Long id){
        dishService.updateStatus(status,id);
        //还要根据菜品id查询分类，简单处理，直接删除以dish_开头的所有缓存
        clearCache("dish_*");
        return Result.success();
    }
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
